#pragma once

/*
 * Simple terminal animations using escape sequences
 */

#ifndef BZB_SLIM

#include "bzbterm/termcolor.h"
#include "bzbstring/stringutils.h"
#include "bzbmem/mem.h"
#include "bzbio/io.h"
#include "bzbterm/termbase.h"


namespace bzbterm {


	struct animation {
		
		using animation_func = std::string(*)(std::string, size_t);

		animation(size_t duration, size_t delta, animation_func func) :
				duration(duration), delta(delta), func(func) {}

		size_t duration {255};	// Animation duration in frames (0 means infinite)
		size_t delta {16};	// Interval between frames in milliseconds
		animation_func func;

		inline void play(std::string input) {

			bzbterm::hide_cursor();
			bzbterm::clear_screen();

			for(size_t i = 0; i < duration - 1 || !duration; ++i) {
				
				bzbio::println(func(input, i));
				bzbterm::clear_screen();
				bzbmem::sleep(delta);
			}

			bzbio::println(func(input, duration));
			bzbmem::sleep(delta);
			bzbterm::show_cursor();
		}

	};
	

	template<size_t r_mul, size_t g_mul, size_t b_mul>
	std::string fade(std::string input, size_t frame) {

		return bzbstring::concat("\033[38;2;",
								frame * r_mul, ";",
								frame * g_mul, ";",
								frame * b_mul, "m",
								input,
								bzbterm::style::reset);
	}


	std::string blink(std::string input, size_t frame) {
		if(frame % 2 == 0) {
			return input;
		} else {
			return std::string(input.size(), ' ');
		}
	}


}

#endif
